package org.example;

import lombok.Data;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.DestroyMode;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class PoolTest {
    public static void main(String[] args) throws Exception {
        GenericObjectPool<FunTester> p = initPool();
        FunTester funTester = p.borrowObject();

        System.out.println();

    }

    static  GenericObjectPool<FunTester> initPool() {
        GenericObjectPoolConfig<FunTester> a = new GenericObjectPoolConfig<>();
        a.setMaxIdle(10);
        a.setMinIdle(2);
        a.setMaxTotal(200);
        return new GenericObjectPool<FunTester>(new FunFactory(), a);
    }

    private static class FunFactory extends BasePooledObjectFactory<FunTester> {

        @Override
        public FunTester create() throws Exception {
            return new FunTester();
        }

        @Override
        public PooledObject<FunTester> wrap(FunTester obj) {
            return new DefaultPooledObject(obj);
        }

        @Override
        public void destroyObject(PooledObject<FunTester> p, DestroyMode destroyMode) throws Exception {
            p.getObject().setName("");
            super.destroyObject(p, destroyMode);
        }
    }

    @Data
    private static class FunTester {
        String name;
    }
}

